﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo.BLL;
using Demo.Model;

namespace Demo.Web
{
    public partial class NewsUI : System.Web.UI.Page
    {
        NewsManager newsManager = new NewsManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            bindgvNews();
        }

        private void bindgvNews()
        {
            List<NewsModel> listNews = newsManager.GetAllNews();
            if (listNews != null)
            {
                gvNews.DataSource = listNews;
                gvNews.DataKeyNames = new string[] { "NewsID" };
                gvNews.DataBind();
            }
        }

        protected void gvNews_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvNews.EditIndex = -1;
            bindgvNews();
        }

        protected void gvNews_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            String newsDate = ((TextBox)gvNews.Rows[e.RowIndex].Cells[3].FindControl("txtNewsDate")).Text.Trim();
            DateTime temp;
            if (!DateTime.TryParse(newsDate, out temp))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('时间格式不对!');", true);
                return;
            }
            NewsModel news = new NewsModel()
            {
                NewsContext = ((TextBox)gvNews.Rows[e.RowIndex].Cells[2].FindControl("txtContext")).Text.Trim(),
                NewsID = Int32.Parse(e.Keys["NewsID"].ToString()),
                NewsTitle = ((TextBox)gvNews.Rows[e.RowIndex].Cells[1].FindControl("txtTitle")).Text.Trim(),
                NewsDate = DateTime.Parse(newsDate)
            };
            if (newsManager.ModityNewsByID(news))
            {
                gvNews.EditIndex = -1;
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('更新成功！');", true);
                bindgvNews();
            }
        }

        protected void gvNews_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvNews.EditIndex = e.NewEditIndex;
            bindgvNews();
        }

        protected void gvNews_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标悬停变色，鼠标移开恢复颜色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor; this.style.backgroundColor='#aef123';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG");
                ((Button)e.Row.FindControl("btnDelete")).Attributes.Add("onclick", string.Format("return confirm('你确定要删除\"{0}\"新闻吗?')", DataBinder.Eval(e.Row.DataItem, "newsTitle")));
                //Object objContext = DataBinder.Eval(e.Row.DataItem, "newsContext");
                //if (objContext != null)
                //{
                //    e.Row.Cells[2].Text = objContext.ToString().Length > 16 ? objContext.ToString().Substring(0, 15) : objContext.ToString();
                //    e.Row.Cells[2].ToolTip = objContext.ToString();
                //}

            }
        }

        protected void gvNews_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "myDelete")
            {
                String strNewsID = e.CommandArgument.ToString();
                if (newsManager.RemoveNewsByID(Int32.Parse(strNewsID)))
                {
                    bindgvNews();
                }
            }
        }

        protected void gvNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNews.PageIndex = e.NewPageIndex;
            bindgvNews();
        }

        protected void btnAddNews_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtNewsTitle.Text) || string.IsNullOrWhiteSpace(txtNewsContext.Text))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('标题、内容均为必填项！');", true);
                return;
            }
            NewsModel news = new NewsModel()
            {
                NewsContext = txtNewsContext.Text.Trim(),
                NewsDate = DateTime.Now,
                NewsTitle = txtNewsTitle.Text.Trim()
            };
            if (newsManager.AddNews(news))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加成功！');", true);
                bindgvNews();
            }

        }

    }
}